package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.anno.PermissionName;
import cn.wolfcode.education.domain.Classroom;
import cn.wolfcode.education.query.ClassroomQuery;
import cn.wolfcode.education.service.IClassroomService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classroom")
public class ClassroomController {
    @Autowired
    private IClassroomService classroomService;

    //跳转到教室界面
    @RequestMapping("")
    @PermissionName("教室管理")
    @RequiresPermissions("classroom:view")
    public String index(){
        return "/classroom";
    }

    //教室列表
    @PermissionName("教室列表")
    @RequiresPermissions("classroom:list")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(ClassroomQuery qo){
        return classroomService.query(qo);
    }

    //教室保存
    @PermissionName("教室保存")
    @RequiresPermissions("classroom:save")
    @RequestMapping("save")
    @ResponseBody
    public JSONResult save(Classroom classroom){
        JSONResult jsonResult = new JSONResult();
        try {
            classroomService.save(classroom);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统崩溃了,攻城狮正在努力攻城!");
        }
        return jsonResult;
    }

    //教室修改
    @PermissionName("教室修改")
    @RequiresPermissions("classroom:update")
    @RequestMapping("update")
    @ResponseBody
    public JSONResult update(Classroom classroom){
        JSONResult jsonResult = new JSONResult();
        try {
            classroomService.update(classroom);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统崩溃了,攻城狮正在努力攻城!");
        }
        return jsonResult;
    }

    @RequestMapping("changeStatus")
    @RequiresPermissions("classroom:changeStatus")
    @PermissionName("教室停用/启用")
    @ResponseBody
    public JSONResult changeStatus(Long id,int flag){

        JSONResult jsonResult = new JSONResult();
        try {
            classroomService.changeStatus(id);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("系统崩溃了,攻城狮正在努力攻城!");
        }
        return jsonResult;
    }
    //教室列表
    @PermissionName("教室列表")
    @RequiresPermissions("classroom:queryAll")
    @RequestMapping("queryAll")
    @ResponseBody
    public List<Classroom> query() {
        return classroomService.queryAll();
    }

    @PermissionName("根据班级id查询教室")
    @RequiresPermissions("classroom:queryClassoomByClassgradeId")
    @RequestMapping("queryClassoomByClassgradeId")
    @ResponseBody
    public List<Classroom> queryClassoomByClassgradeId(Long id) {
        return classroomService.queryClassoomByClassgradeId(id);
    }







}
